//
//  AccountSetting.m
//  TenThousand
//
//  Created by 我的 on 16/6/21.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "AccountSetting.h"
#import "ChangePasswordViewController.h"//修改登录页面
#import "CertificationViewController.h"//实名认证页面
#import "EmailAuthenticationViewController.h"//邮箱验证页面
#import "AddCardViewController.h"//银行卡管理
#import "GesturesViewController.h"//手势
#import "ChangePayViewController.h"//支付密码修改
#import "PersonalDataViewController.h"//个人资料设置
#import "GesturesTableViewCell.h"

@interface AccountSetting ()<UITableViewDataSource,UITableViewDelegate>

@end
#define titleArr @[@"修改登录密码",@"修改支付密码",@"设置个人资料",@"实名认证",@"邮箱认证",@"银行卡管理"]

@implementation AccountSetting

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"账户设置";
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    [self accountTableView];
//    [self setExtraCellLineHidden:_accountTableView];

    
}


- (UITableView *)accountTableView{
    _accountTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStyleGrouped];
    _accountTableView.dataSource = self;
    _accountTableView.delegate = self;
    [self.view addSubview:_accountTableView];
//    _accountTableView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
//     [_accountTableView setSeparatorColor:[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00]];
//    _accountTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _accountTableView.showsVerticalScrollIndicator = NO;
    return _accountTableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return titleArr.count;
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    if (indexPath.section == 2) {
//        GesturesTableViewCell *gestureCell = [tableView dequeueReusableCellWithIdentifier:@"GESTURECELL"];
//        
//        if (gestureCell == nil) {
//            gestureCell = [[[NSBundle mainBundle] loadNibNamed:@"GesturesTableViewCell" owner:nil options:nil] lastObject];
//        }
//        gestureCell.selectionStyle = UITableViewCellSelectionStyleNone;
//        //cell添加阴影
//        gestureCell.layer.shadowOffset =CGSizeMake(0, 1);
//        gestureCell.layer.shadowColor = [UIColor grayColor].CGColor;
//        gestureCell.layer.shadowRadius = 1;
//        gestureCell.layer.shadowOpacity = .5f;
//        return gestureCell;
//    }else{
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    
   
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CELLID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 0 || indexPath.section == 1 || indexPath.section == 3) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = titleArr[indexPath.section];
        cell.textLabel.font = [UIFont systemFontOfSize:17.0f];
        cell.textLabel.textColor = [UIColor colorWithRed:0.56 green:0.56 blue:0.56 alpha:1.00];
        return cell;
//    }
//    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 2 || section == 3 || section == 5) {
        return 10;
    }
    return 0.01;
}

//tableView点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:{
            ChangePasswordViewController *changePassWordView = [[ChangePasswordViewController alloc] init];
            [self setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:changePassWordView animated:YES];
            
        }
            break;
        case 1:{
            
//            ChangePayViewController *changePassWordView = [[ChangePayViewController alloc] init];
//            
//            [self setHidesBottomBarWhenPushed:YES];
//            [self.navigationController pushViewController:changePassWordView animated:YES];
            NSDate *date = [NSDate date];
            NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];//时间格式模板
            dateformatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *dateStr = [dateformatter stringFromDate:date];
            NSString *loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginId"];
            NSDictionary *dic = @{
                                  @"requestType":@"PFT0013",
                                  @"requestId":dateStr,
                                  @"merchantCode":FengFuMerchantCode,
                                  @"userIdIdentity":loginId,
                                  @"noticeUrl":@"www.baidu.com",
                                  @"successReturnUrl":@"",
                                  @"failReturnUrl":@"",
                                  };
            NSString *sign = [[[[[[dic[@"requestType"] stringByAppendingString:dic[@"requestId"]] stringByAppendingString:dic[@"merchantCode"]] stringByAppendingString:dic[@"userIdIdentity"]]  stringByAppendingString:dic[@"noticeUrl"]] stringByAppendingString:dic[@"successReturnUrl"]] stringByAppendingString:dic[@"failReturnUrl"]];
            NSString *signature = [HMACMD5 HMACMD5WithString:sign WithKey:FengFuKEY];
            NSDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dic];
            [dict setValue:signature forKey:@"signature"];
            [SumaPaySDK startService:dict callback:^(id resultDic, NSError *error) {
                NSLog(@"%@--------------%@", resultDic, error);
            }];
        }
            break;
            
//        case 2:{
//            GesturesViewController *gesturView = [[GesturesViewController alloc] init];
//            [self setHidesBottomBarWhenPushed:YES];
//            [self.navigationController pushViewController:gesturView animated:YES];
//        }
//            break;
            
        case 2:{
            PersonalDataViewController *personalView = [[PersonalDataViewController alloc] init];
            [self setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:personalView animated:YES];
        }
            break;
            
        case 3:{
            CertificationViewController *certificationView = [[CertificationViewController alloc] init];
            [self setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:certificationView animated:YES];
            
        }
            break;
            
        case 4:{
            EmailAuthenticationViewController *emailView = [[EmailAuthenticationViewController alloc] init];
            [self setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:emailView animated:YES];
            
        }
            break;
            
        case 5:{
//            AddCardViewController *addCardView = [[AddCardViewController alloc] init];
//            [self setHidesBottomBarWhenPushed:YES];
//            [self.navigationController pushViewController:addCardView animated:YES];
            NSDate *date = [NSDate date];
            NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];//时间格式模板
            dateformatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *dateStr = [dateformatter stringFromDate:date];
            NSString *loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginId"];
            NSDictionary *dic = @{
                                  @"requestType":@"PFT0013",
                                  @"requestId":dateStr,
                                  @"merchantCode":FengFuMerchantCode,
                                  @"userIdIdentity":loginId,
                                  @"noticeUrl":@"www.baidu.com",
                                  @"successReturnUrl":@"",
                                  @"failReturnUrl":@"",
                                  };
            NSString *sign = [[[[[[dic[@"requestType"] stringByAppendingString:dic[@"requestId"]] stringByAppendingString:dic[@"merchantCode"]] stringByAppendingString:dic[@"userIdIdentity"]]  stringByAppendingString:dic[@"noticeUrl"]] stringByAppendingString:dic[@"successReturnUrl"]] stringByAppendingString:dic[@"failReturnUrl"]];
            NSString *signature = [HMACMD5 HMACMD5WithString:sign WithKey:FengFuKEY];
            NSDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dic];
            [dict setValue:signature forKey:@"signature"];
            [SumaPaySDK startService:dict callback:^(id resultDic, NSError *error) {
                NSLog(@"%@--------------%@", resultDic, error);
            }];
        }
            break;
        default:
            break;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 5) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 80)];
        UIButton *exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [view addSubview:exitBtn];
        exitBtn.frame = CGRectMake(20, 20, Screen_Width-40, 40);
        [exitBtn setTitle:@"退出当前账号" forState:UIControlStateNormal];
        [exitBtn setBackgroundImage:[UIImage imageNamed:@"TJ_07"] forState:UIControlStateNormal];
        
        [exitBtn addTarget:self action:@selector(exitClick:) forControlEvents:UIControlEventTouchUpInside];
        return view;
    }
    return nil;
}
-(void)exitClick:(UIButton *)btn{
    
    

    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"LoginId"];
   
   
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadView" object:nil userInfo:nil];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 5) {
        return 80;
    }return 0.001;
}

//隐藏多余的分割线
- (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}


@end
